/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import Model.Connect;
import Pojo.LoaiSanPham;
import Pojo.NhaCungCap;
import Pojo.SanPham;
import Util.HibernateUtil;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author NTB
 */
public class ProductDAO {
    Connect cn;
    public static List<SanPham> View()
    {
        List<SanPham> list=null;
        Session se=HibernateUtil.getSessionFactory().openSession();
        try
        {
            String sql = "from SanPham p\n" +
                        "join p.loaiSanPham as lsp";
            org.hibernate.Query qr= se.createQuery(sql);
            list=qr.list();  
        }
        catch (HibernateException e)
        {
            System.err.print(e);
        }
        finally
        {
            se.close();
        }
        return list;
    }
    
    public static List<SanPham> Search(String name)
    {
        List<SanPham> list=null;
        Session se=HibernateUtil.getSessionFactory().openSession();
        try
        {
            String sql="from SanPham p\n" +
                        "join p.loaiSanPham as lsp\n" +
                        "where p.tensp like :name";
            org.hibernate.Query qr= se.createQuery(sql);
            qr.setString("name", "%"+name+"%");
            list=qr.list();  
        }
        catch (HibernateException e)
        {
            e.printStackTrace();
        }
        finally
        {
            se.close();
        }
        return list;
    }
    
    public static SanPham GetInfo(int sanpham_id)
    {
        SanPham p=null;
        Session se=HibernateUtil.getSessionFactory().openSession();
        try
        {
            p=(SanPham) se.get(SanPham.class, sanpham_id);
        }
        catch (HibernateException e)
        {
            e.printStackTrace();
        }
        finally
        {
            se.close();
        }
        return p;
    }
    
    public static boolean Delete(int sanpham_id)
    {
        Session se=HibernateUtil.getSessionFactory().openSession();
        SanPham p=ProductDAO.GetInfo(sanpham_id);
        if(p==null)
        {
            return false;
        }
        Transaction tr=null;
        try
        {
            tr=se.beginTransaction();
            se.delete(p);
            tr.commit();
        }
        catch (Exception e)
        {
            tr.rollback();
            e.printStackTrace();
        }finally
        {
            se.close();
        }
        return true;
    }   
    
    public static boolean Update(SanPham p)
    {
        Session se=HibernateUtil.getSessionFactory().openSession();        
        if(ProductDAO.GetInfo(p.getMasp()) == null)
        {
            return false;
        }
        Transaction tr=null;
        try
        {
            tr=se.beginTransaction();
            se.update(p);
            tr.commit();
        }
        catch (Exception e)
        {
            tr.rollback();
            e.printStackTrace();
        }finally
        {
            se.close();
        }
        return true;
    }
    
    public static boolean Insert(SanPham p, int mancc, int malsp)
    {
        Session se=HibernateUtil.getSessionFactory().openSession();
        Transaction tr=null;
        try
        {
            tr=se.beginTransaction();
            NhaCungCap ncc = (NhaCungCap) se.get(NhaCungCap.class, mancc);
            LoaiSanPham lsp = (LoaiSanPham) se.get(LoaiSanPham.class, malsp);
            p.setNhaCungCap(ncc);
            p.setLoaiSanPham(lsp);
            se.save(p);
            tr.commit();
        }
        catch (Exception e)
        {
            tr.rollback();
            e.printStackTrace();
        }finally
        {
            se.close();
        }
        return true;
    }
}
